from rest_framework import serializers
from users.models import User


class AdminSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = '__all__'

    def create(self, validated_data):
        admin = super().create(validated_data)

        admin.is_staff = True
        admin.set_password(validated_data.get('password'))
        admin.save()

        return admin

    def update(self, instance, validated_data):
        instance = super().update(instance, validated_data)

        instance.set_password(validated_data.get('password'))
        instance.save()

        return instance
